package com.turnengine.client.local.unit.list.count;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import com.javabi.common.io.data.DataReader;
import com.javabi.common.io.data.DataWriter;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.javabi.common.lang.UncheckedException;
import com.turnengine.client.local.unit.bean.IUnitCount;
import com.turnengine.client.local.unit.bean.IUnitCountSerializer;
import com.turnengine.client.local.unit.bean.UnitCountSerializer;

public class UnitCountListSerializer {

	private final IUnitCountSerializer serializer = new UnitCountSerializer();

	public byte[] serialize(List<IUnitCount> list) {
		try {
			ByteArrayOutputStream output = new ByteArrayOutputStream();
			IDataWriter writer = new DataWriter(output);
			writer.writeObjectCollection(list, false, false, serializer);
			return output.toByteArray();
		} catch (IOException e) {
			throw new UncheckedException(e);
		}
	}

	public List<IUnitCount> deserialize(byte[] data) {
		try {
			IDataReader reader = new DataReader(data);
			return reader.readObjectCollection(null, false, false, serializer);
		} catch (IOException e) {
			throw new UncheckedException(e);
		}
	}

}
